package com.hns.tool.task.taque;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 任务执行
 * @author:wucx
 * @email:zhuangcaijin@126.com
 * @version Revision 2.0.0
 */
public class TaskRun implements Runnable{

	private Task task;
	private Taque taque;
	
	private final Logger logger = LoggerFactory.getLogger(TaskRun.class);
	
	/**
	 * 构造
	 * @param task 后台任务
	 * @param taque 任务执行队列
	 * @email:zhuangcaijin@126.com
	 */
	TaskRun(Task task, Taque taque) {
		this.task = task;
		this.taque = taque;
	}
 	
	/**
	 * 任务执行
	 * @email:zhuangcaijin@126.com
	 */
	public void run() {
		try {
			task.prepare();
			task.execute();
			task.state = Task.State.Done;
		} catch (Exception e) {
			task.state = Task.State.Error;
			logger.error("Task {} ran cause error.", task, e);
		}
		task.endTime = System.currentTimeMillis();
		taque.done(task);
	}

}
